package com.itboye.mengwu.app.ui.mymeng;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.MyBoyAdapter;
import com.itboye.mengwu.app.ui.aizhiwu.ActivityQueryWodeDongTaiDetail;
import com.itboye.mengwu.data.model.AllData;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.MyTieZiCenterBean;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.presenter.MyTieZiCenterPresenter;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.testpic.ui.PublishedActivity;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

public class MyBoyActivity extends BaseActivity implements Observer,
		IXListViewListener {
	ArrayList<TieZiBaoHanReply.GetAList> list;
	MyBoyAdapter adapter;
	XListView listview;
	MyTieZiCenterPresenter lunTanPresenter;
	ImageView back;
	TextView txt_title;
	TieZiBaoHanReply baoHanReply = new TieZiBaoHanReply();
	ArrayList<String> arrayList = new ArrayList<String>();
	private MyTieZiCenterBean bean;
	TieZiPresenter presenter;
	LuntanTieZiBean model = new LuntanTieZiBean();
	int position;
	protected String uid;
	int pageNo = 1;
	String type = "refresh";

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.mybabyfun;
	}

	@Override
	public void onMyCreate() {
		// adapter.notifyDataSetChanged();

		listview.setXListViewListener(this);
		listview.setPullLoadEnable(true);
		listview.setPullRefreshEnable(true);
		showProgressDialog("加载数据中，请稍后", true);
		uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		txt_title.setText("我的宝宝乐");
		lunTanPresenter = new MyTieZiCenterPresenter(this);
		presenter = new TieZiPresenter(this);
		model.setUid(SPUtils.get(this, null, SpPublicParams.UID, "") + "");
		model.setFid("4");
		lunTanPresenter.getInfo(model);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// Intent intent = new Intent(MyBoyActivity.this,
				// ActivityQueryWodeDongTaiDetail.class);
				// intent.putStringArrayListExtra("imgUrl",
				// (ArrayList<String>) baoHanReply.getList().get(position - 2)
				// .getImg());
				// intent.putExtra("messgage",
				// baoHanReply.getList().get(position - 2)
				// .getMessage());
				// intent.putExtra("tid", baoHanReply.getList().get(position -
				// 2)
				// .getTid());
				//
				// intent.putExtra("model", baoHanReply.getList().get(position -
				// 2));
				// startActivityForResult(intent, 101);// (intent);
				// }
				Intent intent = new Intent(MyBoyActivity.this,
						ActivityQueryWodeDongTaiDetail.class);
				intent.putStringArrayListExtra("imgUrl", baoHanReply.getList()
						.get(position - 2).getImg());
				intent.putExtra("message",
						baoHanReply.getList().get(position - 2).getMessage()
								+ "");
				// intent.putExtra("title", aLists.get(position).getSubject());
				intent.putExtra("tid", baoHanReply.getList().get(position - 2)
						.getTid());
				intent.putExtra("isLike",
						baoHanReply.getList().get(position - 2).getIsLike());
				System.out.println("isLike"
						+ baoHanReply.getList().get(position - 2).getIsLike()
						+ "sf");
				startActivityForResult(intent, 101);
			}
		});
		View view = LayoutInflater.from(this).inflate(R.layout.item_day, null);
		RelativeLayout today_lin = (RelativeLayout) view
				.findViewById(R.id.today_lin);
		today_lin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MyBoyActivity.this,

				PublishedActivity.class);
				intent.putExtra("type", "4");
				startActivityForResult(intent, 101);// (intent);
			}
		});
		listview.addHeaderView(view);
		iniAdap();
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == 101 && arg1 == 102) {
			type = "clean";
			model.setUid(SPUtils.get(this, null, SpPublicParams.UID, "") + "");
			pageNo = 1;
			model.setPage_no(pageNo + "");
			model.setFid("4");
			lunTanPresenter.getInfo(model);
		}
	}

	public void iniAdap() {
		list = new ArrayList<>();
		adapter = new MyBoyAdapter(list, this, clickListener);
		listview.setAdapter(adapter);
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == MyTieZiCenterPresenter.QueryMyTieZiSuccess) {
				baoHanReply = (TieZiBaoHanReply) entity.getData();
				if (type.equals("clean")) {
					list.clear();
				}
				list.addAll(baoHanReply.getList());
				adapter.notifyDataSetChanged();
			} else if (entity.getEventType() == MyTieZiCenterPresenter.QueryMyTieZiFail) {

			} else if (entity.getEventType() == TieZiPresenter.TieziDeleteSuccess) {
				// ByAlert.alert(entity.getData() + "");
				model.setFid("4");
				pageNo = 1;
				model.setPage_no(pageNo + "");
				lunTanPresenter.getInfo(model);
			} else if (entity.getEventType() == TieZiPresenter.TieziDeleteFail) {
				// ByAlert.alert(entity.getData() + "");
				adapter.notifyDataSetChanged();
			}
			try {
				closeProgressDialog();
			} catch (Exception e) {
			}
			if (pageNo <= 10) {

			} else {

			}
		}
		stopLoad();
	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			showProgressDialog("正在删除中,请稍后", true);
			position = Integer.parseInt(v.getTag() + "");
			LuntanTieZiBean bean = new LuntanTieZiBean();
			type = "clean";
			bean.setUid(uid);
			bean.setFid("4");
			bean.setTid(list.get(position).getTid());
			presenter.delete(bean);
		}
	};

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		showProgressDialog("正在刷新数据，请稍后", true);
		pageNo = 1;
		type = "clean";
		model.setFid("4");
		model.setPage_no(pageNo + "");
		lunTanPresenter.getInfo(model);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		showProgressDialog("正在刷新数据，请稍后", true);
		type = "refresh";
		pageNo++;
		model.setFid("4");
		model.setPage_no(pageNo + "");
		lunTanPresenter.getInfo(model);
	}

	public void stopLoad() {
		listview.stopLoadMore();
		listview.stopRefresh();
	}

}
